package com.moment.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import com.moment.model.CashierData;
import com.moment.util.Converter;
import com.moment.webservice.Services;
import com.moment.webservice.ServicesProxy;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xml.sax.SAXException;



/**
 * Servlet implementation class CashierController
 */
@WebServlet("/CashierController")
public class CashierController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CashierController() {
		super();
		// TODO Auto-generated constructor stub
	}

	private void viewCashier(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String target = null;
		String cname = request.getParameter("cname");
		String mobilenumber = (String) request.getSession().getAttribute(
				"username");
		if (cname != null && !cname.equals("")) {
			// --New CODE-------------
			Map<String, String> map = new HashMap<String, String>();
			map.put("method", "28");
			map.put("mnum", mobilenumber);
			map.put("cashiername", cname);

			String json = new Converter().mapToJson(map);
			System.out.println("json String :" + json);
			Services serviceproxy = new ServicesProxy();

			String output = serviceproxy.service(json);
			// ---------------------------
			// WebsProxy proxy=new WebsProxy();
			// String output=proxy.cashierDetails(mobilenumber, cname);
			JSONArray jArray;
			JSONObject jobj = null;
			HttpSession session = request.getSession();

			CashierData cashierData = new CashierData();
			System.out.println(output);

			try {
				jArray = new JSONArray(output);
				jobj = jArray.getJSONObject(0);
				System.out.println(jobj.toString());
				cashierData.setName(jobj.getString("name"));
				cashierData.setLocationName(jobj.getString("lname"));
				cashierData.setCashierno(jobj.getString("mobno"));
				cashierData.setDlimit(jobj.getString("dlimit"));
				cashierData.setMlimit(jobj.getString("mlimit"));
				cashierData.setCashierid(jobj.getString("cashierId"));
				cashierData.setmId(jobj.getString("mId"));

				session.setAttribute("cashierObj", cashierData);
				response.sendRedirect("/MMP/jsp/cashierdetails.jsp");

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			target = "jsp/viewlocation.jsp";

		}
		// request.getRequestDispatcher(target).forward(request, response);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	private void addCashier(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String target = null;
		String cname = request.getParameter("cname");
		String ctype = request.getParameter("ctype");
		if (ctype.equalsIgnoreCase("view")) {

			viewCashier(request, response);
		} else {
			String location = request.getParameter("clocation");
			String cpassword = request.getParameter("cpassword");
			String cpassword1 = request.getParameter("cpassword1");
			Passwordvalidator pvalidator = new Passwordvalidator();
			// System.out.println(cpassword+"---"+cpassword1);
			if (!cpassword.equals("")) {
				if (!cpassword.equals(cpassword1)
						|| !pvalidator.validate(cpassword)) {
					target = "jsp/viewcashier.jsp";
					response.sendRedirect(target);
					return;
				}
			}

			String cnum = request.getParameter("cnum");
			String output = null;

			String mobilenumber = (String) request.getSession().getAttribute(
					"username");
			

			if (ctype.equalsIgnoreCase("update")) {
				String cid = request.getParameter("cid");
				String mid = request.getParameter("mid");
				// output=proxy.updateCashier(mobilenumber, cname, cnum,
				// location, cpassword, "5000", "5000", cid, mid);
				// --New CODE-------------
				Map<String, String> map = new HashMap<String, String>();
				map.put("method", "29");
				map.put("mnum", mobilenumber);
				map.put("cashiername", cname);
				map.put("cashiermnum", cnum);
				map.put("locationname", location);
				map.put("password", cpassword);
				map.put("dlimit", "5000");
				map.put("mlimit", "5000");
				map.put("cashierid", cid);
				map.put("mid", mid);

				String json = new Converter().mapToJson(map);
				System.out.println("json String :" + json);
				Services serviceproxy = new ServicesProxy();

				output = serviceproxy.service(json);
				// ---------------------------
			} else {
				// output=proxy.addCashier(mobilenumber, location, cnum, cname,
				// cpassword, "5000", "5000");
				// --New CODE-------------
				Map<String, String> map = new HashMap<String, String>();
				map.put("method", "10");
				map.put("mnum", mobilenumber);
				map.put("locationname", location);
				map.put("cashiermnum", cnum);
				map.put("cashiername", cname);
				map.put("password", cpassword);
				map.put("dlimit", "5000");
				map.put("mlimit", "5000");

				String json = new Converter().mapToJson(map);
				System.out.println("json String :" + json);
				Services serviceproxy = new ServicesProxy();

				output = serviceproxy.service(json);
				// ---------------------------

				System.out.println("location:=" + location);
			}
			System.out.println("data from web Service:=" + output);
			if (output.equals("Success")) {
				
				/*String out = proxy.fetchInstruments(mobilenumber);
				
				System.out.println(out);*/

				target = "jsp/viewcashier.jsp";

			} else {
				target = "jsp/addcashier.jsp";

			}
			// request.getRequestDispatcher(target).forward(request, response);
			response.sendRedirect(target);
		}

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		addCashier(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		addCashier(request, response);
	}

}
